<template>
	<view class="vecode">
		<!-- 状态栏 -->
		<view class="status_bar"></view>
		<!-- 顶部 -->
		<view class="code-top">
			<u-icon name="close" @click="Tologin"></u-icon>
			<view class="top-title">请输入验证码</view>
			<view class="top-txt">验证码已经发送至<text class="mobile">{{ newMobile }}</text></view>
			<view class="top-input">
				<u-message-input mode="box" :breathe="false" :focus="true" inactive-color="#D3D3D3"
					active-color="#D3D3D3" @finish="finish">
				</u-message-input>
				<view class="time-box">
					<view class="times" v-if="times">{{time}}s后重发</view>
					<view class="newtimes" v-else @click="anewtime">重新获取验证码</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { ResCheck, ResSend, ResCommon } from '@/untils/api.js'
	export default {
		data() {
			return {
				time: 60,
				times: true,
				hideMobile: '',
				codes: {
					mobile: '',
					captcha: ''
				}
			}
		},
		onLoad(option) {
			this.codes.mobile = option.mobile
			this.timesout()
		},
		computed: {
			newMobile() {
				return this.codes.mobile.slice(0, 3) + '****' + this.codes.mobile.slice(-4)
			}
		},
		methods: {
			async finish(e) {
				this.codes.captcha = e
				const { data: res } = await ResCheck(this.codes)
				if (res.code != 1) return uni.$showMsg('请输入正确的验证码')
				this.codes.captcha = ''
				// userInfo存储
				this.$store.commit('UpdateUserInfo', res.data.userinfo)
				// token存储
				this.$store.commit('UpdateToken', res.data.userinfo.token)
				this.getCommon()
				// 登录成功跳转
				uni.switchTab({
					url: '/pages/My/My'
				})
			},
			async anewtime() {
				this.times = true
				this.time = 60
				const { data: res } = await ResSend(this.codes.mobile)
				if (res.code != 1) return uni.$showMsg(res.msg)
			},
			// 获取版本号
			async getCommon() {
				const { data: res } = await ResCommon()
				uni.setStorageSync('version', res.data.version)
			},
			Tologin() {
				uni.navigateBack()
			},
			// 倒计时
			timesout() {
				setInterval(() => {
					this.time--
					if (this.time == 0) {
						this.times = false
					}
				}, 1000)
			},

		}
	}
</script>

<style lang="scss" scoped>
	.code-top {
		padding-left: 52rpx;

		.top-title {
			margin-top: 72rpx;
			font-size: 46rpx;
			font-weight: 700;
		}

		.top-txt {
			margin: 14rpx 0 82rpx;
			color: #9A9A9A;
			font-size: 30rpx;

			.mobile {
				margin-left: 15rpx;
				color: #0D0D0D;
				font-weight: 700;
			}
		}

		.top-input {
			display: flex;
			align-items: center;

			.time-box {
				border-left: 1px solid #E5E5E5;
				margin-left: 32rpx;
				padding-left: 33rpx;
				color: #9A9A9A;
				font-size: 26rpx;
			}
		}
	}
</style>
